package cn.qicaiwang.open.websocket.config;

import cn.qicaiwang.open.websocket.endpointer.CrmWebSocketHandler;
import cn.qicaiwang.open.websocket.interceptor.CrmHandshakeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * 开启WebSocket支持
 * @author zhengkai.blog.csdn.net
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebMvcConfigurer, WebSocketConfigurer {
	
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

    @Autowired
    private CrmWebSocketHandler handler;

    @Autowired
    private CrmHandshakeInterceptor interceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        String[] allowsOrigins = { "*" };// 允许连接的域,只能以http或https开头
        registry.addHandler(handler, "/crm/websocket").addInterceptors(interceptor)
                .setAllowedOrigins(allowsOrigins);
    }
}
